package com.betterjr.modules.agreement.service;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.SpringContextHolder;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.modules.risk.loan.entity.LoanProjectElcAgreement;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;
import com.betterjr.modules.risk.loan.service.LoanProjectRelationService;
import com.betterjr.modules.template.entity.ScfContractTemplate;

@Service("scfElecLoanProjectRelationService")
@Scope("singleton")
public class ScfElecLoanProjectRelationService extends ScfElecAgreeLocalService {

    private static final String BUSINTYPE_PREFIX = "loanProjectAgreementBusinTypeCode";

    private static final Logger logger = LoggerFactory.getLogger(ScfElecLoanProjectRelationService.class);

    private LoanProjectRelationService relationService = null;

    @Override
    protected void subInit() {

        this.relationService = SpringContextHolder.getBean(LoanProjectRelationService.class);

    }

    @Override
    protected Map<String, Object> findViewModeData() {

        Map<String, Object> map = new HashMap<>();

        LoanProjectRelation relation = relationService
                .findLoanProjectReltionDetailById(Long.parseLong(this.elecAgree.getRequestNo()));

        BTAssert.notNull(relation, "贷款项目为空，签署失败");

        map.put("template", convertByLoanRelation(relation));
        map.put("loan", relation);
        map.put("transactions", relation.getTransactionRelations());
        map.put("agreement", this.elecAgree);

        return map;
    }

    @Override
    protected String getViewModeFile() {

        return BUSINTYPE_PREFIX + elecAgree.getAgreeType();
    }

    @Override
    public boolean cancelElecAgreement(String anDescribe) {
        logger.info("will cancel appNo is " + this.elecAgree.getAppNo());
        try {

            relationService.saveDeletedLoanById(Long.parseLong(this.elecAgree.getRequestNo()));
        }
        catch (Exception e) {

            logger.info("will cancel appNo is " + this.elecAgree.getAppNo() + "    exception...." + e.getMessage());
            return false;
        }
        /*if (elecAgreeService.checkCancelElecAgreement(this.elecAgree.getAppNo())) {
            return this.elecAgreeService.saveAndCancelElecAgreement(this.elecAgree.getAppNo(), anDescribe);
        }*/
        return true;
    }

    public ScfContractTemplate convertByLoanRelation(LoanProjectRelation relation) {

        /*ContractTemplate template = contractTemplateNewService.findTemplate(anFactoryNo, anBusinType);
        
        if (template == null || null == template.getTemplate() || template.getTemplate().equals(0L)) {
            BTAssert.notNull(null, "尚未上传合同模版");
        }*/

        LoanProjectElcAgreement agreement = convertLoanElcAgreement(relation, this.elecAgree.getAgreeType());

        ScfContractTemplate scfTemplate = new ScfContractTemplate();
        scfTemplate.setBatchNo(agreement.getBatchNo());
        scfTemplate.setFactorName(relation.getFactoryName());
        scfTemplate.setFactorNo(relation.getFactoryNo());
        scfTemplate.setTemplateName(agreement.getAggrementName());
        scfTemplate.setTemplateType(LoanProjectElcAgreement.convertTypeToBusinType(agreement.getType()));

        return scfTemplate;

    }

    private LoanProjectElcAgreement convertLoanElcAgreement(LoanProjectRelation relation, String agreeType) {

        return relation
                .getElcAgreementByType(LoanProjectElcAgreement.convertBusinTypeToType(BUSINTYPE_PREFIX + agreeType));
    }

}
